import { encrypt, decrypt } from 'crypto-js/aes';
import { parse } from 'crypto-js/enc-utf8';
import pkcs7 from 'crypto-js/pad-pkcs7';
import ECB from 'crypto-js/mode-ecb';
import UTF8 from 'crypto-js/enc-utf8';

export class AesEncryption {
  constructor({
    key,
    iv,
  }) {
    this.key = parse(key);
    this.iv = parse(iv);
  }
  updateConfig({
    key,
    iv,
  }) {
    this.key = parse(key);
    this.iv = parse(iv);
  }
  getOptions() {
    return {
      mode: ECB,
      padding: pkcs7,
      iv: this.iv,
    }
  }
  encryptByAES(cipherText) {
    return encrypt(cipherText, this.key, this.getOptions()).toString();
  }
  decryptByAES(cipherText) {
    return decrypt(cipherText, this.key, this.getOptions()).toString(UTF8);
  }
}
